/**
 * 
 */
package com.stulsoft.yssmugmugutils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * @author Yuriy Stul
 * 
 */
public class Program {

	private final String username;
	private final String password;
	private final Request request;

	private static Logger logger;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		logger = Logger.getLogger(Program.class);

		// Assign log for org.apache.commons 
		System.setProperty("org.apache.commons.logging.Log",
				"org.apache.commons.logging.impl.SimpleLog");

		if (args.length < 3) {
			usage();
			return;
		}

		String username = args[0];
		String password = args[1];
		String request = args[2];

		boolean hasError = false;
		if (username == null || username.length() == 0) {
			System.out.println("Incorrect username: " + username);
			hasError = true;
		}

		if (password == null || password.length() == 0) {
			System.out.println("Incorrect password: " + password);
			hasError = true;
		}

		if (request == null || request.length() == 0) {
			System.out.println("Incorrect request: " + request);
			hasError = true;
		}

		hasError = !isRequestSupported(request);

		if (hasError) {
			usage();
			return;
		}

		String message = "Username: " + username + ", password: " + password
				+ ", request: " + request;
		System.out.println(message);
		logger.info(message);

		Program program = new Program(username, password, request);
		program.run();

		message = "Completed.";
		System.out.println(message);
		logger.info(message);
	}

	private static void usage() {
		System.out.println("Usage:");
		System.out.println("run.bat <username> <password> <request>");
		System.out.println("Where:");
		System.out.println("\t<username> - the user name in Smugmug");
		System.out.println("\t<password> - the password in Smugmug");
		System.out.println("YSMumugUtils's may have value:");
		System.out.println("\tbuild_tree");
	}

	private static boolean isRequestSupported(String request) {
		try {
			return (Request.valueOf(request) != null);
		} catch (IllegalArgumentException ex) {
			System.out.println("Unsupported request: " + request);
			return false;
		}
	}

	private Program(String username, String password, String request) {
		this.username = username;
		this.password = password;
		this.request = Request.valueOf(request);
	}

	private void run() {
		switch (request) {
		case build_tree:
			try {
				TreeBuilder treeBuilder = new TreeBuilder(username, password);
				treeBuilder.buildTree();
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;

		default:
			System.out.println("Unsupported request: " + request);
			usage();
			break;
		}

	}

	/**
	 * Returns the instance of the Properties for specified file name.
	 * 
	 * @param configName
	 *            - Specifies the file name.
	 * @return the instance of the Properties for specified file name.
	 * @throws FileNotFoundException
	 *             The file was not found.
	 * @throws IOException
	 *             I/O error was occurred during reading properties.
	 */
	public static Properties getProperties(String configName)
			throws FileNotFoundException, IOException {
		String thePath = getPathToConfig(configName);

		Properties properties = new Properties();
		properties.load(new FileInputStream(thePath));

		return properties;
	}

	/**
	 * Returns a path to configuration file.
	 * 
	 * @param fileName
	 *            - the configuration file.
	 * @return the path to configuration file.
	 */
	public static String getPathToConfig(String fileName) {
		String classpath = System.getProperty("java.class.path").split(
				File.pathSeparator)[0];

		// Getting parent path
		File theFile = new File(classpath);
		String parentFolder = theFile.getParent();

		String thePath = "";
		if (parentFolder != null) {
			thePath = parentFolder + File.separatorChar;
		}

		thePath += fileName;

		return thePath;
	}

	public static void markProcess(){
		System.out.print(".");
	}
}
